#!/bin/sh

set -e

NAME=listener
PIDFILE=/home/flashbot/$NAME.pid
DAEMON=/bin/bash
DAEMON_OPTS='-c "poetry run python listener.py"'

case "$1" in
  start)
        echo -n "Starting daemon: "$NAME
	start-stop-daemon \
        --background \
        --chdir /app \
        --chuid flashbot \
        --start \
        --quiet \
        --pidfile $PIDFILE \
        --make-pidfile \
        --startas /bin/bash -- -c "poetry run python listener.py"
        echo "."
	;;
  stop)
        echo -n "Stopping daemon: "$NAME
	start-stop-daemon --stop --quiet --oknodo --pidfile $PIDFILE
    rm $PIDFILE
        echo "."
	;;
  tail)
        tail -f listener.log
	;;
  restart)
        echo -n "Restarting daemon: "$NAME
	start-stop-daemon --stop --quiet --oknodo --retry 30 --pidfile $PIDFILE
    rm $PIDFILE
	start-stop-daemon \
        --background \
        --chdir /app \
        --chuid flashbot \
        --start \
        --quiet \
        --pidfile $PIDFILE \
        --make-pidfile \
        --startas /bin/bash -- -c "poetry run python listener.py"
        echo "."
    ;;

  *)
	echo "Usage: "$1" {start|stop|restart|tail}"
	exit 1
esac

exit 0
